package de.guidoludwig.jatt.util;

public class CommandlineProgress {
	
	private int currentProgress;
	private int lineLength;
	
	public CommandlineProgress() {
		this(78);
	}
	public CommandlineProgress(int lineLength) {
		this.lineLength = lineLength;
	}
	
	public void progress(int percentage, String message) {
		currentProgress = percentage;
		System.out.print("\r" + makeProgress() + " " + currentProgress + "% " + makeComment(message));
	}
	
	private String makeComment(String original) {
		int rest = lineLength - 28;
		String comment = original.length() < rest ? original : original.substring(0, rest);
		while (comment.length() < rest) {
			comment += " ";
		}
		return comment;
	}
	
	private String makeProgress() {
		String progress = "";
		int nextP = 0;
		int steps = 5;
		while (nextP < 100) {
			nextP += steps;
			if (nextP > currentProgress) {
				progress += " ";
			} else {
				progress += "=";
			}
		}
		return "|" + progress + "|";
	}

}
